﻿/************************************************
* Copyright (C) 2011 东北农业大学(NEAU) 版权所有。
* 文件名：
* 版本号：
* 文件功能描述：
* 作者：
* 创建日期:
* 版本号：
* 修改人：
* 修改日期：
* 修改描述：
************************************************/
Ext.BLANK_IMAGE_URL = '/script/ext/resources/images/s.gif';
Ext.onReady(function () {
    Ext.QuickTips.init();

    //创建Role根节点
    var treeRoleRoot = new Ext.tree.AsyncTreeNode({ text: '角色列表', id: 'treeRoleRoot', expanded: true, leaf: false });
    //创建Role树面板
    var treeRoleMenu = new Ext.tree.TreePanel({
        loader: new Ext.tree.TreeLoader({ dataUrl: "SERVER/role/RoleTree.aspx" }),
        id: 'treeRoleMenu',
        region: 'west',
        split: true,
        width: 180,
        minSize: 160,
        maxSize: 400,
        collapsible: true,
        margins: '5 0 5 5',
        cmargins: '5',
        rootVisible: false,
        lines: true,
        autoScroll: true,
        enableDD: true,
        title: "角色列表",
        tbar: [{
            iconCls: 'icon-group_add-16', text: "新建", tooltip: '新建', handler: function () {
                Ext.Msg.prompt("新建角色", "新角色名称：", function (btn, text) {
                    if (btn == "ok") {
                        Ext.Ajax.request({
                            url: 'SERVER/role/RoleAdd.aspx',
                            success: function (data) {
                                var json = Ext.util.JSON.decode(data.responseText);
                                if (json.success) {
                                    Ext.Msg.alert("完成", json.msg);
                                    treeRoleMenu.loader.load(treeRoleRoot);
                                } else {
                                    Ext.Msg.alert("失败", json.msg);
                                }
                            },
                            failure: function () {
                                Ext.Msg.alert("消息", "请求失败");
                            },
                            params: { roletext: text }
                        });
                    }
                });
            }
        }, {
            iconCls: 'icon-group_edit-16', text: "编辑", tooltip: '编辑', handler: function () {
                var model = treeRoleMenu.getSelectionModel();
                var selectNode = model.getSelectedNode();
                if (selectNode) {
                    Ext.Msg.prompt("编辑角色", "新角色名称：", function (btn, text) {
                        if (btn == "ok") {
                            if (text != selectNode.text) {
                                Ext.Ajax.request({
                                    url: 'SERVER/role/RoleAdd.aspx',
                                    success: function (data) {
                                        var json = Ext.util.JSON.decode(data.responseText);
                                        if (json.success) {
                                            Ext.Msg.alert("完成", json.msg);
                                            treeRoleMenu.loader.load(treeRoleRoot);
                                        } else {
                                            Ext.Msg.alert("失败", json.msg);
                                        }
                                    },
                                    failure: function () {
                                        Ext.Msg.alert("消息", "请求失败");
                                    },
                                    params: { roletext: text, roleid: selectNode.id }
                                });
                            } else {
                                Ext.Msg.alert("消息", "没用更新");
                            }
                        }
                    }, "", "", selectNode.text);
                }
            }
        }, {
            iconCls: 'icon-group_delete-16', text: "删除", tooltip: '删除', handler: function () {
                Ext.Msg.confirm("警告", "确认要删除此角色数据？", function (btn) {
                    if (btn == "yes") {
                        var model = treeRoleMenu.getSelectionModel();
                        var selectNode = model.getSelectedNode();
                        if (selectNode) {
                            Ext.Ajax.request({
                                url: 'SERVER/role/RoleDel.aspx',
                                success: function (data) {
                                    var json = Ext.util.JSON.decode(data.responseText);
                                    if (json.success) {
                                        Ext.Msg.alert("完成", json.msg);
                                        treeRoleMenu.loader.load(treeRoleRoot);
                                    } else {
                                        Ext.Msg.alert("失败", json.msg);
                                    }
                                },
                                failure: function () {
                                    Ext.Msg.alert("消息", "请求失败");
                                },
                                params: { roleid: selectNode.id }
                            });
                        }
                    }
                });
            }
        }]
    });
    //设置树的父节点为
    treeRoleMenu.setRootNode(treeRoleRoot);
    //创建根节点
    var treeModelRoot = new Ext.tree.AsyncTreeNode({ text: '全部功能模块', id: 'treeModelRoot', expanded: true, leaf: false });
    //创建树面板
    var treeModelMenu = new Ext.tree.TreePanel({
        loader: new Ext.tree.TreeLoader({ url: "SERVER/role/MenuTreeAll.aspx" }),
        id: 'treeModelMenu',
        region: 'center',
        margins: '5 5 5 0',
        lines: true,
        rootVisible: false,
        autoScroll: true,
        enableDD: true,
        title: "权限配置（请双击左侧角色列表进行权限配置）",
        bodyStyle: "padding:5px;",
        animCollapse: false,
        animate: false,
        tbar: [{ iconCls: 'icon-expand-all',
            text: '全部展开',
            handler: function () {
                RootExpandALL(treeModelRoot);
            }
        }, { iconCls: 'icon-collapse-all',
            text: '全部折叠',
            handler: function () {
                treeModelRoot.collapseChildNodes(true);
            }
        }, { iconCls: 'icon-all',
            text: '全选',
            handler: function () {
                treeModelRoot.eachChild(function (child) {
                    child.ui.toggleCheck(true);
                    child.attributes.checked = true;
                    child.fireEvent('checkchange', child, true);
                });
            }
        }, { iconCls: 'icon-noall',
            text: '全不选',
            handler: function () {
                treeModelRoot.eachChild(function (child) {
                    child.ui.toggleCheck(false);
                    child.attributes.checked = false;
                    child.fireEvent('checkchange', child, false);
                });
            }
        }, { iconCls: 'icon-save-16',
            text: '保存',
            handler: function () {
                var roleid = $("#roleId").val();
                if (roleid != "0") {
                    var ck = treeModelMenu.getChecked();
                    if (ck.length > 0) {
                        var checkid = new Array; //存放选中id的数组
                        for (var i = 0; i < ck.length; i++) {
                            checkid.push(ck[i].id); //添加id到数组
                        }
                        var parames = new Array();
                        if (checkid.toString() != "") {
                            var ids = new Array();
                            var newids = new Array();
                            ids = checkid.toString().split(',');
                            for (var i = 0; i < ids.length; i++) {
                                var kIndex = ids[i].indexOf('-');
                                if (kIndex > 0) {
                                    newids.push(ids[i].substring(0, kIndex) + ":" + ids[i].substring(kIndex + 1, ids[i].length));
                                } else {
                                    newids.push(ids[i] + ":S");
                                }
                            }
                            var newidslist1 = new Array();
                            var qs = new Array();
                            var q = "";
                            for (var i = 0; i < newids.length; i++) {
                                var newidslist = new Array();
                                newidslist = newids[i].split(':');
                                if (!IsE(newidslist1, newidslist[0])) {
                                    newidslist1.push(newidslist[0]);
                                }
                            }
                            for (var i = 0; i < newidslist1.length; i++) {
                                var q = "";
                                for (var j = 0; j < newids.length; j++) {
                                    var newidslist = new Array();
                                    newidslist = newids[j].split(':');
                                    if (newidslist[0] == newidslist1[i]) {
                                        q += newidslist[1];
                                    }
                                }
                                qs.push(q);
                            }
                            for (var i = 0; i < newidslist1.length; i++) {
                                parames.push(newidslist1[i] + ":" + qs[i]);
                            }
                            ExtAjaxRequest('SERVER/role/MenuTreeAllSave.aspx', { roleid: $("#roleId").val(), pvs: parames.toString() });
                        } else {
                            Ext.Msg.confirm("警告", "确认要清空当先角色的全部权限？", function (btn) {
                                if (btn == "yes") {
                                    ExtAjaxRequest('SERVER/role/MenuTreeAllSave.aspx', { roleid: $("#roleId").val(), pvs: "" });
                                }
                            });
                        }
                    } else {
                        Ext.Msg.alert("提示", "请分配权限");
                    }
                } else {
                    Ext.Msg.alert("提示", "请选择角色");
                }
            }
        }]
    });
    //设置Model树的父节点为treeModelRoot
    treeModelMenu.setRootNode(treeModelRoot);
    treeModelMenu.on('checkchange', function (node, checked) {
        node.expand();
        node.attributes.checked = checked;
        node.eachChild(function (child) {
            child.ui.toggleCheck(checked);
            child.attributes.checked = checked;
            child.fireEvent('checkchange', child, checked);
        });
    }, treeModelMenu);

    //为树创建单击事件
    treeRoleMenu.on('dblclick', function (node, e) {
        if (node.isLeaf()) {
            e.stopEvent();
            //重新加载数据源
            treeModelMenu.setTitle(node.text + " | 权限配置 (请双击左侧角色列表进行权限配置)");
            $("#roleId").val(node.id);
            var loader = new Ext.tree.TreeLoader({ dataUrl: 'SERVER/role/MenuTreeAll.aspx?roleid=' + node.id + "&time=" + new Date().getTime() });
            loader.load(treeModelRoot, function () {

                treeModelMenu.expandAll();
                var ck = treeModelMenu.getChecked();
                var checkid = new Array; //存放选中id的数组
                for (var i = 0; i < ck.length; i++) {
                    checkid.push(ck[i].id); //添加id到数组
                } $("#pvs").val(checkid.toString());
            });
        }
    });

    //创建页面布局
    var viewport = new Ext.Viewport({ layout: 'border', items: [treeRoleMenu, treeModelMenu] });
    viewport.doLayout();

    setTimeout(function () { Ext.get('loading').remove(); Ext.get('loading-mask').fadeOut({ remove: true }); }, 400);
}); 